package dome1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 王圆豪
 * Date: 2024-11-15
 * Time: 15:17
 */

import java.util.LinkedList;
import java.util.Queue;

class TreeNode {
    char value; // 节点值
    TreeNode left; // 左子树
    TreeNode right; // 右子树

    // 构造函数
    public TreeNode(char value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

class BinaryTree {
    TreeNode root;

    // 根据先序和中序遍历序列创建二叉树
    public TreeNode buildTree(String preorder, String inorder) {
        if (preorder == null || inorder == null || preorder.length() == 0 || inorder.length() == 0) {
            return null;
        }
        return buildTreeHelper(preorder, inorder, 0, preorder.length() - 1, 0, inorder.length() - 1);
    }

    // 辅助方法，用于递归构建二叉树
    private TreeNode buildTreeHelper(String preorder, String inorder, int preStart, int preEnd, int inStart, int inEnd) {
        if (preStart > preEnd || inStart > inEnd) {
            return null;
        }

        // 根节点是先序序列的第一个元素
        char rootVal = preorder.charAt(preStart);
        TreeNode root = new TreeNode(rootVal);

        // 在中序序列中找到根节点的位置
        int rootIndexInInorder = inorder.indexOf(rootVal, inStart);

        // 计算左右子树的大小
        int leftSize = rootIndexInInorder - inStart;

        // 递归构建左右子树
        root.left = buildTreeHelper(preorder, inorder, preStart + 1, preStart + leftSize, inStart, rootIndexInInorder - 1);
        root.right = buildTreeHelper(preorder, inorder, preStart + leftSize + 1, preEnd, rootIndexInInorder + 1, inEnd);

        return root;
    }

    // 输出二叉树的括号表示法
    public String toBracketString() {
        return toBracketString(root);
    }

    private String toBracketString(TreeNode node) {
        if (node == null) {
            return "";
        }
        String result = String.valueOf(node.value);
        if (node.left != null || node.right != null) {
            result += "(" + toBracketString(node.left) + ")";
            if (node.right != null) {
                result += "(" + toBracketString(node.right) + ")";
            }
        }
        return result;
    }

    // 先序遍历
    public void preOrderTraversal() {
        preOrderTraversal(root);
        System.out.println();
    }

    private void preOrderTraversal(TreeNode node) {
        if (node == null) {
            return;
        }
        System.out.print(node.value + " ");
        preOrderTraversal(node.left);
        preOrderTraversal(node.right);
    }

    // 中序遍历
    public void inOrderTraversal() {
        inOrderTraversal(root);
        System.out.println();
    }

    private void inOrderTraversal(TreeNode node) {
        if (node == null) {
            return;
        }
        inOrderTraversal(node.left);
        System.out.print(node.value + " ");
        inOrderTraversal(node.right);
    }

    // 后序遍历
    public void postOrderTraversal() {
        postOrderTraversal(root);
        System.out.println();
    }

    private void postOrderTraversal(TreeNode node) {
        if (node == null) {
            return;
        }
        postOrderTraversal(node.left);
        postOrderTraversal(node.right);
        System.out.print(node.value + " ");
    }

    // 层次遍历
    public void levelOrderTraversal() {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            System.out.print(node.value + " ");
            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
        }
        System.out.println();
    }
}
public class Main {
    public static void main(String[] args) {
        // 先序序列和中序序列
        String preorder = "ABDECFG";
        String inorder = "DBEAFCG";

        BinaryTree tree = new BinaryTree();
        // 创建二叉树
        tree.root = tree.buildTree(preorder, inorder);

        // 输出二叉树的括号表示法
        System.out.println("Bracket Notation: " + tree.toBracketString());

        // 先序遍历
        System.out.print("Preorder Traversal: ");
        tree.preOrderTraversal();

        // 中序遍历
        System.out.print("Inorder Traversal: ");
        tree.inOrderTraversal();

        // 后序遍历
        System.out.print("Postorder Traversal: ");
        tree.postOrderTraversal();

        // 层次遍历
        System.out.print("Level-order Traversal: ");
        tree.levelOrderTraversal();
    }
}